<?php
$action = empty( $_REQUEST['ajax'] ) ? '' : strtolower( $_REQUEST['ajax'] );
if($action){
    if($action=='js'){
        $ContentType ='application/x-javascript';
    }else{
        $ContentType ='text/plain';
    }
}else{
    $ContentType ='text/html';
}
header("Content-Type:".$ContentType.";charset=utf-8");


/**


*/

function site_foot_html(){
    $src= <<<EOHTM
<style type="text/css">
div.ft,div.ft a {color:#aaa;}
</style>
<div class="ft" style="text-align:center">
<a href="http://www.shdic.com/mzsm.html">免责声明</a>
版权所有© 2005-2016 上海开拓信息 <a href="http://www.shdic.com" target="new">shdic.com</a> 保留所有权利。 <a href="http://www.miibeian.gov.cn" target="_blank">沪ICP备12034829号-1</a>&nbsp;网站改版中，欢迎邮件联系 kevinshen2014@163.com
EOHTM;
    return $src.PHP_EOL.site_51la_html();
}

function site_51la_html(){
    $src= <<<EOHTM
<script language="javascript" type="text/javascript" src="http://js.users.51.la/18779107.js"></script>
<noscript><a href="http://www.51.la/?18779107" target="_blank"><img alt="&#x6211;&#x8981;&#x5566;&#x514D;&#x8D39;&#x7EDF;&#x8BA1;" src="http://img.users.51.la/18779107.asp" style="border:none" /></a></noscript>
EOHTM;
    return $src;
}

function v( $str )
{
    return isset( $_REQUEST[$str] ) ? $_REQUEST[$str] : false;
}



function linear_search(&$a, $new, $length)
{
    $flag = false;  //assuming $new is not in $array     
    /* if the $array is empty */
    if($length == 0)
        return $flag;
     
    for($i = 0; $i < $length; $i++){
        if($a[$i] == $new){
            $flag = true;
            break;
        }
    }         
    return $flag;
}

function Kmod($bn, $sn)
{
 return intval(fmod(floatval($bn), $sn));
}

function rand_map_100($cell_style=null){
 	$arr=array();
    $random = mt_rand(1, 100);     
    for($i = 0; $i < 100; $i++){
        while(linear_search($arr, $random, $i))
            $random = mt_rand(1, 100);
             
        $arr[$i] = $random;
    }

    $src='';
	$src2='';
	$colorArr=array('f4b300','78ba00','2673ec','ae113d','632f00','b01e00','4e0038','c1004f','7200ac','2d004e','006ac1','001e4e','008287','004d60','004a00','00c13f','15992a','ff981d','e56c19','b81b1b','ff1d77','b81b6c','aa40ff','691bb8','1faeff','1b58b8','56c5ff','569ce3','00d8cc','00aaaa','91d100','b81b6c','e1b700','d39d09','ff76bc','e064b7','00a4a4','ff7d23','4cafb5','044d91','832772','d15a44','de971b','017802','6e2ea0');

    /* output the $array */
    // $tab1 ="<table width='600px' height='400px' style=\"background-image:url('./static/app/map600.jpg');background-repeat:no-repeat; background-attachment: fixed;\" cellspacing='0px' border='1px'>";
    $tab2 ="<table width='600px' height='400px' class='mc' cellspacing='0px' border='1px'>";
    for($i = 0; $i < count($arr); $i++){
    	if( Kmod($i,10)==0){
    		$src .= "<tr height='40px'>".PHP_EOL;	
    	}    	
    	if(isset($arr[$i])){
    		if(strlen($arr[$i])<2){
    			$num = '0'.$arr[$i];
    		}else{
    			$num = $arr[$i];
    		}
    		if($cell_style===null){
    			$s='style="background:#'.$colorArr[mt_rand(0,count($colorArr)-1)].'"';
    		}else{
    			$s=$cell_style;
    		}
			$src .= "<td width='60px' ".$s.">#" . $num . "</td>";
    	}else{
			$src .= "<td>.</td>";	
    	}
        // $src .= "<tr><td>NO.$i</td><td>" . $arr[$i] . "</td></tr>";
        if( Kmod($i,10)==9){
    		$src .= "</tr>".PHP_EOL;	
    	}
    }
    $src .="</table>";
    // return $tab1.$src.PHP_EOL.$tab2.$src.PHP_EOL;
    return $tab2.$src.PHP_EOL;
}


function rand_table($x=10,$y=10,$cell_height=40,$cell_width=60,$cell_style=null){
    $arr=array();
    $max = $x*$y;
    $random = mt_rand(1,$max );
    for($i = 0; $i < $max; $i++){
        while(linear_search($arr, $random, $i))
            $random = mt_rand(1, $max);
             
        $arr[$i] = $random;
    }

    $src='';
    $src2='';
    $colorArr=array('f4b300','78ba00','2673ec','ae113d','632f00','b01e00','4e0038','c1004f','7200ac','2d004e','006ac1','001e4e','008287','004d60','004a00','00c13f','15992a','ff981d','e56c19','b81b1b','ff1d77','b81b6c','aa40ff','691bb8','1faeff','1b58b8','56c5ff','569ce3','00d8cc','00aaaa','91d100','b81b6c','e1b700','d39d09','ff76bc','e064b7','00a4a4','ff7d23','4cafb5','044d91','832772','d15a44','de971b','017802','6e2ea0');

    /* output the $array */
    // $tab1 ="<table width='600px' height='400px' style=\"background-image:url('./static/app/map600.jpg');background-repeat:no-repeat; background-attachment: fixed;\" cellspacing='0px' border='1px'>";

    $tab2 ="<table width='".intval($cell_width*$x)."px' height='".intval($cell_height*$y)."px' class='mc' cellspacing='0px' border='1px'>";
    for($i = 0; $i < count($arr); $i++){
        if( Kmod($i,$x)==0){
            $src .= "<tr height='".$cell_height."px'>".PHP_EOL;   
        }       
        if(isset($arr[$i])){
            if(strlen($arr[$i])<2){
                $num = '0'.$arr[$i];
            }else{
                $num = $arr[$i];
            }
            if($cell_style===null){
                $s='style="background:#'.$colorArr[mt_rand(0,count($colorArr)-1)].'"';
            }else{
                $s=$cell_style;
            }
            $src .= "<td width='".$cell_width."px' ".$s.">#" . $num . "</td>";
        }else{
            $src .= "<td>.</td>";   
        }
        // $src .= "<tr><td>NO.$i</td><td>" . $arr[$i] . "</td></tr>";
        if( Kmod($i,$x)==($x-1) ){
            $src .= "</tr>".PHP_EOL;    
        }
    }
    $src .="</table>";
    // return $tab1.$src.PHP_EOL.$tab2.$src.PHP_EOL;
    return $tab2.$src.PHP_EOL;
}


function rand_table_v2($opt_arr,$x=10,$y=10,$cell_height=40,$cell_width=60){
    $src='';
    $src2='';
    $colorArr=array('f4b300','78ba00','2673ec','ae113d','632f00','b01e00','4e0038','c1004f','7200ac','2d004e','006ac1','001e4e','008287','004d60','004a00','00c13f','15992a','ff981d','e56c19','b81b1b','ff1d77','b81b6c','aa40ff','691bb8','1faeff','1b58b8','56c5ff','569ce3','00d8cc','00aaaa','91d100','b81b6c','e1b700','d39d09','ff76bc','e064b7','00a4a4','ff7d23','4cafb5','044d91','832772','d15a44','de971b','017802','6e2ea0');

    if(isset($opt_arr['rand_status_arr']) ){    
        if(!is_array($opt_arr['rand_status_arr'])){
            die('Error !'.__METHOD__.' Error options arrar()! #rand_status_arr');  
        }
        
        $rs_arr=array();
        foreach ($opt_arr['rand_status_arr'] as $key => $value) {
            // echo '$key='.$key.' , $value='.$value;
            $rs_arr[]=$colorArr[mt_rand(0,count($colorArr)-1)];
        }
        $rs_max=count($rs_arr);
    }else{
        $rs_max=0;
    }
    $rand_cell_bg=false;
    if(isset($opt_arr['rand_cell_bg']) ){
        $rand_cell_bg=$opt_arr['rand_cell_bg'];
    }

    $arr=array();
    $max = $x*$y;
    
    if(isset($opt_arr['hide_rand_num']) && $opt_arr['hide_rand_num']==true ){
        for($i = 0; $i < $max; $i++){                 
            $arr[$i] = $i+1;
        }
    }else{
        $random = mt_rand(1,$max );
        for($i = 0; $i < $max; $i++){
            while(linear_search($arr, $random, $i))
                $random = mt_rand(1, $max);
                 
            $arr[$i] = $random;
        }
    }
    

    $tab2 ="<table width='".intval($cell_width*$x)."px' height='".intval($cell_height*$y)."px' class='mc' cellspacing='0px' border='1px'>";
    for($i = 0; $i < count($arr); $i++){
        if( Kmod($i,$x)==0){
            $src .= "<tr height='".$cell_height."px'>".PHP_EOL;   
        }       
        if(isset($arr[$i])){
            if(strlen($arr[$i])<2){
                $num = '0'.$arr[$i];
            }else{
                $num = $arr[$i];
            }
            $s='';
            if($rand_cell_bg){
                $s='style="background:#'.$colorArr[mt_rand(0,count($colorArr)-1)].'"';
            }else if($rs_max>0 ){
                $tmp_idx=mt_rand(0,$rs_max-1);
                if( !empty($opt_arr['rand_status_arr'][$tmp_idx]) ){
                    if(isset($opt_arr['rand_status_color']) ){
                        if($opt_arr['rand_status_color']=='auto'){
                            $cc = $rs_arr[$tmp_idx];
                            $s='style="background:#'.$cc.'" ';    
                        }else{
                            $s='style="background:#'.$opt_arr['rand_status_color'].'" '; 
                        }
                    }
                    // $num = '<a href="#" title="'.$opt_arr['rand_status_arr'][$tmp_idx].'">'.$num.'</a>';
                    $num = $num.'&nbsp;('.$opt_arr['rand_status_arr'][$tmp_idx].')';
                }else{
                    // echo "skip";
                }
            }
            $src .= "<td width='".$cell_width."px' ".$s.">#" . $num . "</td>";
        }else{
            $src .= "<td>.</td>";   
        }
        // $src .= "<tr><td>NO.$i</td><td>" . $arr[$i] . "</td></tr>";
        if( Kmod($i,$x)==($x-1) ){
            $src .= "</tr>".PHP_EOL;    
        }
    }
    $src .="</table>";
    // return $tab1.$src.PHP_EOL.$tab2.$src.PHP_EOL;
    return $tab2.$src.PHP_EOL;
}


function rand_list($max=20,$prefix='',$endfix='<br/>'){
    $arr=array();
    $random = mt_rand(1,$max );
    for($i = 0; $i < $max; $i++){
        while(linear_search($arr, $random, $i)){
            $random = mt_rand(1, $max);
        }             
        $arr[$i] = $random;
    }

    $src='';
    for($i = 0; $i < count($arr); $i++){
        if(isset($arr[$i])){
            if(strlen($arr[$i])<2){
                $num = '0'.$arr[$i];
            }else{
                $num = $arr[$i];
            }
            
        }else{
            $num = '.';
        }
        $src .= $prefix.$num.$endfix. PHP_EOL;
    }
    return $src.PHP_EOL;
}


/**

*/

$a='adf403';
$tt = v('a') ;  if($tt) $a=$tt;
if(strlen($a)<=3){
    die('403 Access Denied/Forbidden');
}

switch ($a) {
case 'randmap100':
	echo rand_map_100('');
    echo site_foot_html();
	die();	
	break;

case 'randmap100bg':
	echo rand_map_100();
    echo site_foot_html();
	die();	
	break;

case 'randtabletest':
    $opt_arr=array('rand_status_arr'=>array('天气','地形','怪物','政治','战乱','','','','','','','')
        ,'rand_status_color'=>'ccc'
        ,'hide_rand_num'=>true
        );
    echo rand_table_v2($opt_arr,10,10);
    echo site_foot_html();
    die();  
    break;

case 'randmapstatus':
    $opt_arr=array('rand_status_arr'=>array('天气','地形','怪物','政治','战乱','','','','','','','')
        ,'rand_status_color'=>'ccc'
        ,'hide_rand_num'=>true
        );
    echo rand_table_v2($opt_arr,10,10);    
    echo site_foot_html();
    die();  
    break;

case 'randtable12':
    echo rand_table(6,2,40,60,'');
    echo '<hr/>'.PHP_EOL;
    echo rand_table(6,2,40,60);
    echo '<hr/>'.PHP_EOL;
    echo rand_table(12,1,40,60,'');
    echo '<hr/>'.PHP_EOL;
    echo rand_table(12,1,40,60);
    echo site_foot_html();
    die();  
    break;

case 'randtable20':
    echo rand_table(4,5,40,60,'');
    echo '<hr/>'.PHP_EOL;
    echo rand_table(4,5);
    echo site_foot_html();
    die();  
    break;

case 'randlist':
    echo rand_list();
    echo site_foot_html();
    die();  
    break;

case 'randlist12':
    echo rand_list(12);
    echo site_foot_html();
    die();  
    break;

case 'randlist100':
    echo rand_list(100);
    echo site_foot_html();
    die();  
    break;

default:
	die('403 Access Denied/Forbidden');
}

die('403 Access Denied/Forbidden #');

?>